package app;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

import tools.*;
import tools.shapes.*;

import java.net.*;

@SuppressWarnings("serial")
public class ObjectDraw extends JApplet {

	protected ControlPanelView controlPanel;
	protected DrawingCanvas drawingCanvas;
	protected ToolBarView toolBar;
	protected ToolList toolList;
	protected MenuBarView menuBar;
	protected boolean isApplet = false;
	
	  /****< Constructors and Initializers >***************************************/
	  public ObjectDraw(boolean isApplet) { 
	    this.isApplet = isApplet;
	    if (!isApplet) {
	      init();
	    }
	  }

	  public ObjectDraw() {
	    /* invoked as Applet */
	    this(true);
	  }

	  /**
	   * Invoked implicitly if MiniDraw is executed as an applet.  Otherwise, the
	   * constructor will explicitly call this method to initialize and display all 
	   * of the MiniDraw components. 
	   */
	  public void init() {
	    getContentPane().setLayout(new BorderLayout());
	    drawingCanvas = new DrawingCanvas();
	    getContentPane().add(drawingCanvas, BorderLayout.CENTER);
	    controlPanel = new ControlPanelView(drawingCanvas);
	    getContentPane().add(controlPanel, BorderLayout.SOUTH);
	    toolList = createToolList();
	    toolBar = new ToolBarView(toolList);
	    getContentPane().add(toolBar, BorderLayout.WEST);
	    menuBar = new MenuBarView(toolList);
	    getContentPane().add (menuBar, BorderLayout.NORTH);
	    
	    this.addComponentListener(new ResizeHandler());
	  }
	  
	  protected ToolList createToolList() {
		    ToolList actions = new ToolList();
		    
		    actions.add(
		      new ToolController("Freehand",
		  	                     getImageIcon("freehand.jpg"),
		  	                     "freehand drawing tool",
		  	                     drawingCanvas,
		  	                     new FreehandTool(drawingCanvas)));

		    actions.add(
		  		new ToolController("Line",
		  		getImageIcon("line.jpg"),
		  		"Line drawing tool",
		  		drawingCanvas,
		  		new TwoEndShapeTool(drawingCanvas, new LineShape())));
		  
		    actions.add(
		  		new ToolController("Rectangle",
		  		getImageIcon("rectangle.jpg"),
		  		"Rectangle drawing tool",
		  		drawingCanvas,
		  		new TwoEndShapeTool(drawingCanvas, new RectangleShape())));
		    
		    actions.add(
			  		new ToolController("Filled Rectangle",
			  		getImageIcon("filledrectangle.jpg"),
			  		"Filled rectangle drawing tool",
			  		drawingCanvas,
			  		new TwoEndShapeTool(drawingCanvas, new RectangleFillShape())));
		  		
		    actions.add(
		  	    new ToolController("Oval",
		  	    getImageIcon("oval.jpg"),
		  		"Oval drawing tool",
		  		drawingCanvas,
		  		new TwoEndShapeTool(drawingCanvas, new OvalShape())));
		    
		    actions.add(
		    	new ToolController("Filled Oval",
		    	getImageIcon("filledoval.jpg"),
		    	"Filled oval drawing tool",
		    	drawingCanvas,
		    	new TwoEndShapeTool(drawingCanvas, new OvalFillShape())));

		    actions.add(
		  		new ToolController("Text",
		  		getImageIcon("text.jpg"),
		  		"text drawing tool",
		        drawingCanvas,
		  		new TextTool(drawingCanvas)));	
		    
		    actions.add(
		  		new ToolController("Eraser",
		  		getImageIcon("eraser.jpg"),
		  		"Eraser drawing tool",
		  		drawingCanvas,
		  		new EraserTool(drawingCanvas)));
		  
		    actions.add(
		    	new ToolController("Select",
		    	getImageIcon("select.jpg"),
		    	"Selection tool",
		    	drawingCanvas,
		    	new SelectTool(drawingCanvas)));
		    
		    return actions;
		  }// end protected ToolList createToolList()
	  
	  protected ImageIcon getImageIcon(String fileName) {
		    URL url;
			if(isApplet) {
		      try {
		        url = new URL(getCodeBase(), fileName);    
		      } catch(MalformedURLException e) {
		        return null;
		      }
		      return new ImageIcon( url );
		    }// end executed as applet
		    else {
		      return new ImageIcon( fileName );
		    }// end executed as application    
		  }// end protected ImageIcon getImageIcon( String )
		  
	
	public static void main(String[] argv) {
	    JFrame frame = new JFrame();
	    frame.setTitle("ObjectDraw");
	    frame.getContentPane().setLayout(new BorderLayout());
	    frame.getContentPane().add(new ObjectDraw(false),
				      BorderLayout.CENTER);
	    frame.addWindowListener(new AppCloser());
	    frame.pack();
	    frame.setSize(900, 800);
	    frame.setVisible(true);
	}
	
	  /****< AppCloser >***********************************************************/

	  /**
	   * Inner class for terminating the application.
	   *
	   * When executed as an application, closing the window does not necessarily
	   * trigger application termination. This class catches the window closing
	   * event and terminates the application.
	   */
	  static class AppCloser extends WindowAdapter  {
	    public void windowClosing(WindowEvent e) {
	       System.exit(0);
	    }
	  }// end static class AppCloser extends WindowAdapter
	  
	  private class ResizeHandler implements ComponentListener {

		public void componentHidden(ComponentEvent arg0) {
			// nothing happens
		}

		public void componentMoved(ComponentEvent arg0) {
			// nothing happens
		}

		public void componentResized(ComponentEvent arg0) {
			drawingCanvas.getimageBufferGraphics().setFont(new Font("Serif", Font.BOLD, 24));
			drawingCanvas.redrawAll();
		}

		public void componentShown(ComponentEvent arg0) {
			// nothing happens
		}
		  
	  }
	
}
